/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Administrator
 */
public class MainFrame extends JFrame{
    JButton load,create;
    Box main;
    Eventer ev = new Eventer();
    
    public MainFrame(String s) {
        super(s);
        load = new JButton("Load Test");
        load.addActionListener(ev);
        create = new JButton("Create Test");
        create.addActionListener(ev);
        main = Box.createVerticalBox();
        main.add(Box.createHorizontalStrut(15));
        HCenter(main,load);
        main.add(Box.createHorizontalStrut(10));
        HCenter(main,create);
        main.add(Box.createHorizontalStrut(15));
        
        this.add(main);
//        this.pack();
        this.setSize(100, 100);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    
    public MainFrame() {
        this("What are u doin'?");
    }
    
    static void HCenter (JComponent toAdd, JComponent toBeAdded) {
        Box b = Box.createHorizontalBox();
        b.add(Box.createHorizontalGlue());
        b.add(toBeAdded);
        b.add(Box.createHorizontalGlue());
        toAdd.add(b);
    }
    
    
    private static String toOneString(String[] s,String divisor) {
        String aux = "";
        for(String a:s)
            aux+=a + divisor;
        return aux;
    }
    
    
    
    private class Eventer implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton b = (JButton) e.getSource();
            if(b==load) {
                JFileChooser fc = new JFileChooser();
                fc.setAcceptAllFileFilterUsed(true);
                FileManager.addFilter(fc,".txt","Simple Text Files");
                fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fc.showOpenDialog(MainFrame.this);
                File f = fc.getSelectedFile();
                if(f==null) return;
                String[] s = toOneString(FileManager.getFullText(f),"").split(Prova.encode(sep, 3));
                ArrayList<Question> q = new ArrayList<Question>(s.length);
                for(int i=0;i<s.length;i++)
                    q.add(Question.fromString(Prova.decode(s[i], 3)));
                new LoadFrame(q);
                MainFrame.this.setVisible(false);
            } else if(b==create) {
                new CreateFrame();
                MainFrame.this.setVisible(false);
            }
        }
        
        private static final String sep = "#3#";
        
    }
}
